#ifndef TCP_CONNECTION_H
#define TCP_CONNECTION_H
#include <boost/asio.hpp>
#include "global.h"
#include <list>
#include <mutex>
#include <atomic>
#define HEAD_LEN 4




struct msg_head{
    unsigned short message_id;
    unsigned short body_len;
};





class tcp_connection:public std::enable_shared_from_this<tcp_connection>{

public:
    tcp_connection(boost::asio::io_context & _io_context);
    void async_read_head();
    void async_read_body();
    void async_write_msg(u_short message_id,u_short body_len, const char *body);
    boost::asio::ip::tcp::socket &get_socket();
    ~tcp_connection();
    //用户数据
    std::string email;
private:

    boost::asio::ip::tcp::socket _socket;
    //需要缓冲区来接收发来的包
    char head_buffer[HEAD_LEN];   //
    char *body_buffer;
    unsigned short now_message_id;
    unsigned short now_body_len;
    bool is_close;

    //回包怎么处理
    std::list<std::pair<u_short,const char *>>send_list;
    std::mutex send_list_mtx;
    std::atomic<bool> is_sending;   //是否在发包
    void doing_send_pkg(std::pair<u_short,const char *> _pair);

    //发送出去的包等待回应的包



};


#endif